// pages/cart/cart.js
Page({
  data: {
    globalData: getApp().globalData,
    carts: [],
    doneTotal: 0,
    priceTotal: 0,
    
    // 设置开始的位置
    startX: 0,
    startY: 0,
  },

  onLoad(options) {
    if(wx.getStorageSync('user') == '') {
      wx.showModal({
        title: '提示',
        content: '请登录查看',
        complete: (res) => {
          if (res.confirm) {
            wx.navigateTo({url: '/pages/login/login',})
          }
        }
      })
    }
  },
  onShow() {
    this.getCarts()
  },
  onPullDownRefresh() {
    this.getCarts()
  },

  getCarts() {
    let that = this
    wx.request({
      url: that.data.globalData.server + '/cart/query',
      method: 'GET',
      data: {userId: wx.getStorageSync('user').userId},
      success(res) {
        that.setData({
          carts: res.data.data
        })
      },
      complete() {
        that.setData({
          doneTotal: that.data.carts.reduce((pre, cart) => pre + (cart.done ? 1 : 0), 0)
        })
        that.getPriceTotal()
      }
    })
  },
  editCount(item) {
    let that = this
    wx.request({
      url: that.data.globalData.server + '/cart',
      method: 'PUT',
      data: {
        cartId: item.cartId,
        count: item.count
      },
      success(res) {
        that.getCarts()
      }
    })
  },
  editDone(e) {
    let item = e.currentTarget.dataset.item
    let that = this
    wx.request({
      url: that.data.globalData.server + '/cart',
      method: 'PUT',
      data: {
        cartId: item.cartId,
        done: !item.done
      },
      success(res) {
        that.getCarts()
      },
    })
  },
  minus(e) {
    let item = e.currentTarget.dataset.item
    if(item.count > 1) {
      item.count -= 1
      this.editCount(item)
    } 
  },
  plus(e) {
    let item = e.currentTarget.dataset.item
    item.count += 1
    this.editCount(item)
  },
  getPriceTotal() {
    let total = 0
    this.data.carts.forEach(cart => {
      if(cart.done) total += cart.reprice * cart.count
    })
    this.setData({priceTotal: total})
  },
  checkAll() {
    let isAll = this.data.doneTotal === this.data.carts.length
    let that = this
    wx.request({
      url: that.data.globalData.server + '/cart',
      method: 'PUT',
      data: {
        userId: wx.getStorageSync('user').userId,
        done: !isAll
      },
      success(res) {
        that.getCarts()
      },
    })
  },
  toProduct(e) {
    let id = e.currentTarget.dataset.id
    wx.navigateTo({url: '/pages/productDetail/productDetail?id=' + id})
  },
  toTotal() {
    let list = []
    this.data.carts.forEach(item => {if(item.done) list.push(item)})
    wx.navigateTo({url: '/pages/total/total?list=' + JSON.stringify(list)})
  },

  // 开始滑动
  touchStart(e) {
    let carts = [...this.data.carts]
    carts.forEach(item => {
      if (item.isTouchMove) {
        item.isTouchMove = !item.isTouchMove;
      }
    });
    this.setData({
      carts: carts,
      startX: e.touches[0].clientX,
      startY: e.touches[0].clientY
    })
  },
  touchMove(e) {
    let moveX = e.changedTouches[0].clientX;
    let moveY = e.changedTouches[0].clientY;
    let indexs = e.currentTarget.dataset.index;
    let carts = [...this.data.carts]

    let angle = this.angle({
      X: this.data.startX,
      Y: this.data.startY
    }, {
      X: moveX,
      Y: moveY
    });

    carts.forEach((item, index) => {
      item.isTouchMove = false;
      // 如果滑动的角度大于30° 则直接return；
      if (angle > 30) {
        return
      }

      if (indexs === index) {
        if (moveX > this.data.startX) { // 右滑
          item.isTouchMove = false;
        } else { // 左滑
          item.isTouchMove = true;
        }
      }
    })
    this.setData({
      carts
    })
  },

  /**
   * 计算滑动角度
   * @param {Object} start 起点坐标
   * @param {Object} end 终点坐标
   */
  angle: function (start, end) {
    var _X = end.X - start.X,
      _Y = end.Y - start.Y
    //返回角度 /Math.atan()返回数字的反正切值
    return 360 * Math.atan(_Y / _X) / (2 * Math.PI);
  },

  // 删除
  delItem(e) {
    let id = e.currentTarget.dataset.id;
    let that = this
    wx.request({
      url: that.data.globalData.server + '/cart/' + id,
      method: 'DELETE',
      success(res) {
        that.getCarts()
      }
    })
  }
})
